package com.yellowpage.ca.service;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

import com.yellowpage.ca.dao.UserGroupDao;
import com.yellowpage.ca.dao.UserUsergroupDao;
import com.yellowpage.ca.data.UserGroup;
import com.yellowpage.ca.data.UserUsergroup;
import com.yellowpage.white.common.PageRender;
import com.yellowpage.white.dao.QueryAssistantSQL;
import com.yellowpage.white.service.BaseServiceImpl;

public class UserGroupServiceImpl extends BaseServiceImpl implements
		UserGroupService {
	private UserGroupDao userGroupDao;
	private UserUsergroupDao userUsergroupDao;

	public void setUserGroupDao(UserGroupDao userGroupDao) {
		this.userGroupDao = userGroupDao;
	}

	/* white generate Method */
	public UserGroup getUserGroup(Long id) {
		return userGroupDao.get(id);
	}

	public List<UserGroup> getAllUserGroup() {
		return userGroupDao.getAll();
	}

	public List<Map> queryUserGroupByPage(Map queryMap, PageRender pageRender) {

		QueryAssistantSQL sqlAssistant = baseDao
				.newQueryAssistantSQL(
						"SELECT a.USER_GROUP_ID ,  a.NAME , a.DISPLAY_NAME , a.STATUS, "
								+ "       (select display_name from CA_USER where user_id = a.last_update_user) as last_update_user, "
								+ "       date_format(a.last_update_time,'%Y-%m-%d %T') as last_update_time, "
								+ "       (select display_name from CA_USER where user_id = a.create_user) as create_user,"
								+ "       date_format(a.create_time,'%Y-%m-%d %T') as create_time "
								+ "   FROM CA_USER_GROUP a WHERE 1=1 ")
				.addCondition(" and NAME like :name ").addCondition(
						" and DISPLAY_NAME like :displayName ");

		List<Map> resultList = sqlAssistant.queryByPage(
				baseDao.getConnection(), queryMap, pageRender);

		return resultList;
	}

	public void saveUserGroup(UserGroup userGroup) {
		userGroupDao.save(userGroup);
	}

	public void updateUserGroup(UserGroup userGroup) {
		userGroupDao.update(userGroup);
	}

	public void delete(List deleteList) {
		userGroupDao.remove(deleteList);
	}

	public List<Map> getUserWithGroup(Long userGroupId, boolean isInGroup) {
		QueryAssistantSQL sqlAssistant = baseDao
				.newQueryAssistantSQL("select u.user_id, u.name, u.display_name "
						+ " from CA_USER u "
						+ "  where "
						+ (isInGroup ? "" : "not")
						+ " exists(select ug.user_usergroup_id from CA_USER_USERGROUP ug "
						+ " where ug.user_id = u.user_id and ug.user_group_id = "
						+ userGroupId + ")");

		List<Map> resultList = sqlAssistant
				.query(baseDao.getConnection(), null);

		return resultList;
	}

	public void saveGroupUsers(List<Long> userIdList, Long userGroupId,
			Long loginUserId) {

			List<Long> userUsergroupIds = userUsergroupDao.removeByUsergroup(userGroupId);
			userUsergroupDao.remove(userUsergroupIds);

			List<UserUsergroup> userUsergroups = new ArrayList();
			Date createTime = new Date();
			for (Long userId : userIdList) {
				UserUsergroup userUsergroup = new UserUsergroup();
				userUsergroup.setUserId(userId);
				userUsergroup.setUserGroupId(userGroupId);
				userUsergroup.setCreateTime(new Timestamp(createTime.getTime()));
				userUsergroup.setCreateUser(loginUserId);

				userUsergroups.add(userUsergroup);
			}

			userUsergroupDao.save(userUsergroups);
			
		
	}

	public UserUsergroupDao getUserUsergroupDao() {
		return userUsergroupDao;
	}

	public void setUserUsergroupDao(UserUsergroupDao userUsergroupDao) {
		this.userUsergroupDao = userUsergroupDao;
	}

}